package com.extension.company.view.income;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.contract.MyFootprintContract;
import com.extension.company.presenter.my.MyFootprintPresenter;
import com.extension.company.common.widget.BounceBackViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;

public class MyFootprint extends BaseTitleActivity<MyFootprintContract.View, MyFootprintPresenter>
        implements MyFootprintContract.View, View.OnClickListener {

    @BindViews({R.id.tv_tag1, R.id.tv_tag2})
    List<TextView> tags;

    @BindView(R.id.viewpager)
    BounceBackViewPager viewpager;

    private List<Fragment> listFragments;
    private FragmentRecently fragmentRecently;
    private FragmentCommission fragmentCommission;

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, MyFootprint.class);
        activity.startActivity(intent);
    }

    @Override
    protected void initData() {
        setTitle("我的足迹");
        fragmentRecently = new FragmentRecently();
        fragmentCommission = new FragmentCommission();
        listFragments = new ArrayList<Fragment>();
        listFragments.add(fragmentRecently);
        listFragments.add(fragmentCommission);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        tags.get(0).setOnClickListener(this);
        tags.get(1).setOnClickListener(this);
        setOnPageChangeListener();
    }

    private void setOnPageChangeListener() {
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                setPagerStatus(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public int getMainLayout() {
        return R.layout.myfootprint;
    }

    @Override
    public MyFootprintContract.View createView() {
        return this;
    }

    @Override
    public MyFootprintPresenter createPresenter() {
        return new MyFootprintPresenter();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tag1:
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv_tag2:
                viewpager.setCurrentItem(1);
                break;
        }
    }

    //适配器
    private class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return listFragments.get(arg0);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listFragments.size();
        }
    }

    private void setPagerStatus(int temp) {
        tags.get(0).setTextColor(getResources().getColor(R.color.tvc23));
        tags.get(1).setTextColor(getResources().getColor(R.color.tvc23));
        if (temp == 0) {
            tags.get(0).setTextColor(getResources().getColor(R.color.topcolor));
        }else if (temp == 1) {
            tags.get(1).setTextColor(getResources().getColor(R.color.topcolor));
        }
    }
}
